Page({
  data: {
    currentMonth: new Date().getMonth() + 1,
    currentYear: new Date().getFullYear(),
    daysInMonth: []
  },

  onLoad: function() {
    this.generateCalendar();
  },

  prevMonth: function() {
    if (this.data.currentMonth === 1) {
      this.setData({
        currentMonth: 12,
        currentYear: this.data.currentYear - 1
      });
    } else {
      this.setData({
        currentMonth: this.data.currentMonth - 1
      });
    }
    this.generateCalendar();
  },

  nextMonth: function() {
    if (this.data.currentMonth === 12) {
      this.setData({
        currentMonth: 1,
        currentYear: this.data.currentYear + 1
      });
    } else {
      this.setData({
        currentMonth: this.data.currentMonth + 1
      });
    }
    this.generateCalendar();
  },

  generateCalendar: function() {
    let date = new Date(this.data.currentYear, this.data.currentMonth - 1);
    let daysInMonth = [];
    let firstDay = new Date(date.getFullYear(), date.getMonth(), 1).getDay();
    let totalDays = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();

    for (let i = 0; i < firstDay; i++) {
      daysInMonth.push({ date: '' });
    }

    for (let i = 1; i <= totalDays; i++) {
      daysInMonth.push({ date: i });
    }

    this.setData({
      daysInMonth: daysInMonth
    });
  },

  getDayClass: function(item) {
    let today = new Date();
    let currentDay = new Date(this.data.currentYear, this.data.currentMonth - 1, item.date);
    if (item.date === '' ) {
      return 'empty-day';
    } else if (today.toDateString() === currentDay.toDateString()) {
      return 'today';
    } else if (currentDay.getDay() === 0 || currentDay.getDay() === 6) {
      return 'weekend';
    } else {
      return 'weekday';
    }
  }
})